package io.gitee.wminyu.day03.Sort;

import java.util.Arrays;
import java.util.Scanner;

public class Bubble_sort {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int m = input.nextInt();
        int arrays[] = new int[m];
        for (int i=0; i< m; i++){
            arrays[i] = input.nextInt();
        }
        System.out.println("原数组为："+Arrays.toString(arrays));
        Bubble(arrays,m);
    }
    //冒泡排序
    public static void Bubble(int [] numble,int n){
        int temp;

        for(int i=0; i<n-1; i++){
            int flag=1;
            for (int j =0; j<n-1-i;j++){
                if (numble[j]>numble[j+1]){
                    flag=0;
                    temp = numble[j];
                    numble[j] = numble[j+1];
                    numble[j+1] = temp;
                }
            }
            if (flag==1)        //如果做完第i趟发现数组为正序，
                break;          //退出，不必输出结果，也不再参与后续排序
            else {
                System.out.print("第"+i+"趟排序为：");
                Display(numble);
                System.out.println();
            }
        }
        System.out.println("最终排序为："+Arrays.toString(numble));
    }
    public static void Display(int[] a){
        for (int i=0;i<a.length;i++){

            System.out.print(a[i]+" ");
        }
    }
}
